from sklearn.datasets import load_iris
from sklearn.feature_selection import SelectKBest, f_classif
import pandas as pd
import numpy as np

# 加载示例数据集
data = load_iris()
X, y = data.data, data.target
# print(X)

sk = SelectKBest(k=2)
X_new = sk.fit_transform(X,y)
# print(X_new)

selected_features = sk.get_support(indices=True)
#获取所选特征的掩码或整数索引。
print(selected_features)
scores = sk.scores_[selected_features]
print(scores)
df_new = pd.DataFrame(X_new, columns=np.array(data.feature_names)[[2,3]])
print(df_new)